--
-- Author: shunguo.chen
-- Date: 2017-11-06 16:21:19
--
-- BoxController  开（幸运）宝箱

local BoxView = import("..views.BoxView")
local BoxPreView = import("..views.BoxPreView")
local ExpBoxView = import("..views.ExpBoxView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(params)
	self.super.ctor(self, params)

end

function ClassRef:init()
	self._model = app:getInst("RewardModel")

	-- 开宝箱中
	self._isInOpenBox = false
end

function ClassRef:removeListeners()
	self.super.removeListeners(self)
end

function ClassRef:listEvent()
    return {
    	"onLoginedNoticeOver",
    	-- "onMapChanged",
	}
end

function ClassRef:onEvent(eventName, data)
	if eventName == "onLoginedNoticeOver" then
		self:checkHaveDungeonBox()
	elseif eventName == "onMapChanged" then
		-- 副本中未来得及开启的宝箱，地图传送结束后需要继续弹出
		-- if not data then
		-- 	self:checkHaveDungeonBox()
		-- end
	end
end

-- 宝箱次数不足界面
function ClassRef:_openPreBoxView(data)
    -- local view = BoxPreView.new( app.mvc.VIEWTYPE_BLUR, data )
    local view = BoxPreView.new( nil, data )
    view:onCloseView(function(sender)
        self:removeChild(sender)
    end)
    self:addChild(view)
    view:show()
end

-- data = {
--  	boxType=Enums.BoxType,
--  	dungeonID=number/nil, 副本id(副本宝箱)
--  	type=number/nil, 装备宝箱／宝物宝箱（副本宝箱）
--  	guildChallengeID=number/nil, 工会挑战id （工会宝箱）
--  	championType=number/nil, 冠军赛奖励类型（冠军赛宝箱）
--  	seasonLv=number/nil, 冠军赛奖励类型（锦标赛赛季宝箱）
-- }
function ClassRef:openView(data)
	if not data then return end

	-- 获取剩余次数
	local restTimes = UD:getLuckBoxRestTimesByType(data.boxType)
	if restTimes <= 0 then -- 不存在剩余奖励次数
		self:_openPreBoxView( data )
		return
	end

	-- 如果是地下城，需判断宝箱类型
	if data.boxType == Enums.BoxType.dungeon then
		--self._model:addDungeonBoxType(data)

		-- 保存当前的地下城宝箱id（不要了，先屏蔽）
		--UD:setLuckboxId(data.dungeonID)
	end

    local boxSceneType
    if data.boxType == Enums.BoxType.championSeason or data.boxType == Enums.BoxType.guildChallenge or data.boxType == Enums.BoxType.associate 
		or data.boxType == Enums.BoxType.challengeSeason 
		or data.boxType == Enums.BoxType.eliteArenaSeason 
		or data.boxType == Enums.BoxType.eliteDuelSeason then
        boxSceneType = Enums.BoxSceneType.pvpReward
    else
        boxSceneType = Enums.BoxSceneType.luckBox
    end

	if cc.isDead(self._view) then
		-- self._view = BoxView.new(app.mvc.VIEWTYPE_BLUR, data)
		self._view = BoxView.new(nil, data, boxSceneType)
		self._view:onCloseView(handler(self, self.closeView))
		self._view:onClearView(handler(self, self.clearView))
		self._view:onOperate(handler(self, self.onOperate))
		self:addChild(self._view, {notDelete=true})
	end

	self._view:show()
end
function ClassRef:closeView()
	if self._view then
		local data = self._view:getData()
		local closeType = self._view:getCloseType()
		self:removeChild(self._view)

		--[[ if data.boxType == Enums.BoxType.dungeon then
			if closeType == 1 then
				-- 宝箱打开过之后本地清理
				UD:removeLuckboxId()
			elseif closeType == -1 then
				-- 宝箱取消要通知服务器
				self._model:reqGiveUpBox("dunEquipment")
			end
		end ]]
	end
end
function ClassRef:clearView()
	if self._view then
		self._view = nil
	end

	-- 开宝箱结束，发送升级相关消息
	self._isInOpenBox = false
	UD.player:setApplyEnable(true)
	UD.bag:setApplyEnable(true)
	display.setToastEnable(true)
	app:sendMsg("PlayerController", "setGetExpEnable", true)
end

function ClassRef:isInOpenBox()
	return self._isInOpenBox
end

-- 登录后检测是否有未开启的宝箱, 开启第一个
function ClassRef:checkHaveDungeonBox()
	local restTimes = UD:getLuckBoxRestTimesByType(Enums.BoxType.dungeon)
	if restTimes <= 0 then return end

	local luckboxId = UD:getLuckboxId()
	if luckboxId > 0 then
		local data = {boxType=Enums.BoxType.dungeon, dungeonID=luckboxId}
		self:openView(data)
	end
end

-- 宝箱打开
function ClassRef:onOpenBox(luckItems)
	if not self:isViewAlive() then return end
	self._view:openBox(luckItems)
end

-- 宝箱打开失败
function ClassRef:openBoxFailedCallBack()
	if not self:isViewAlive() then return end
	-- 开宝箱失败，发送升级相关消息
	self._isInOpenBox = false
	UD.player:setApplyEnable(true)
	UD.bag:setApplyEnable(true)
	display.setToastEnable(true)
	app:sendMsg("PlayerController", "setGetExpEnable", true)
	self:closeView()
end

---------------------------- 经验卡相关 -----------------------------------
function ClassRef:closeExpBoxView(data)
	if self._expBoxView then
		self:removeChild(self._expBoxView, data)
	end
end
function ClassRef:clearExpBoxView()
	if self._expBoxView then
		self._expBoxView = nil
	end
end
-- 打开经验卡
function ClassRef:onOpenExpBox( data )
	if nil == self._expBoxView then
		self._expBoxView = ExpBoxView.new(nil, data)
		self._expBoxView:onCloseView(handler(self, self.closeExpBoxView))
		self._expBoxView:onClearView(handler(self, self.clearExpBoxView))
		self._expBoxView:onOperate(handler(self, self.onOperate))
		self:addChild(self._expBoxView)
	end
	self._expBoxView:show()
	app:sendMsg( "PlayerController", "showExpBar", SceneZOrder.popupUIZOrder + 1 )
end
-- 打开经验卡成功回调
function ClassRef:gotExpBoxRewardSuccess()
	if self._expBoxView and self._expBoxView:isVisible() then
		self._expBoxView:playSuccessAnim()
	end
end
----------------------------经验卡相关-----------------------------------

----------------------------view logic-----------------------------------
function ClassRef:onOperate(op, param)
	-- print("BoxController.onOperate", op)
	if op == "openBox" then
		local data = {}
		if param.boxType == Enums.BoxType.dungeon then
			data.type = "dunJewel"--(param.type == "jewel") and "dunJewel" or "dunEquipment"
			data.dungeonID = param.dungeonID
		elseif param.boxType == Enums.BoxType.secret then
			data.type = "secret"
			data.taskIndex = param.taskIndex
		elseif param.boxType == Enums.BoxType.pvp then
			data.type = "arenaJewel"
			data.aranaID = param.aranaID
		elseif param.boxType == Enums.BoxType.guildChallenge then -- 公会挑战
			data.type = "guildChallenge"
			data.guildChallengeID = param.guildChallengeID
		elseif param.boxType == Enums.BoxType.eventBox then -- 世界活动副本宝箱
			data.type = "eventBox"
		elseif param.boxType == Enums.BoxType.champion then -- 冠军赛
			data.type = "champion"
			data.championType = param.championType
		elseif param.boxType == Enums.BoxType.championSeason then -- 锦标赛赛季宝箱
			data.type = "championSeason"
		elseif param.boxType == Enums.BoxType.challengeSeason then -- 挑战赛赛季宝箱
			data.type = "challengeSeason"
		elseif param.boxType == Enums.BoxType.associate then -- 使用关联道具
			data.type = "associate"
			data.uid = param.itemUid
			data.id = param.itemId
		elseif param.boxType == Enums.BoxType.eliteArenaSeason then -- 精英竞技场赛季宝箱
			data.type = "eliteMatch"
			data.playId = Enums.TeamTarget.eliteMatchArena
		elseif param.boxType == Enums.BoxType.eliteDuelSeason then -- 精英决斗场赛季宝箱
			data.type = "eliteMatch"
			data.playId = Enums.TeamTarget.eliteMatchDuel
		end

		-- 先暂停升级相关消息发送，等开宝箱结束再操作
		self._isInOpenBox = true
		UD.player:setApplyEnable(false)
		UD.bag:setApplyEnable(false)
		display.setToastEnable(false)
		app:sendMsg("PlayerController", "setGetExpEnable", false)

		self._model:reqOpenBox(data)
	elseif op == "openExpBox" then -- 打开经验宝箱
		self._model:reqGotExpBoxReward( param )
	end
end

return ClassRef
